<?php

class Welcome extends SDO_Controller {

	private $userinfo	= array();

	function Welcome()
	{
		parent::Controller();
		header("Content-type:text/html;charset=utf-8");
		$this->load->model("admin");
		$this->load->helper("ip");
		if (!ip_helper::is_company())
			die("非公司网络，不允许登录！！！");
	}

	function index()
	{
		redirect('/welcome/admin');
	}

	function login()
	{
		$this->load->view('login');
	}

	function logout()
	{
		session_destroy();
		redirect('welcome/login');
	}
	function check()
	{
		$username		=  iconv('UTF-8','GB18030',$this->getpara("username"));
		$passwd			= $this->getpara("password");
		$RemberMe		= $this->getpara('RemberMe');
		$RemberMePass	= $this->getpara('RemberMePass');
		$dyn = $this->getpara('dnypassword');
		if ( empty($username) || empty($passwd))
		{
			die(json_encode(array("success" => false, "msg"=>"用户名密码不能为空")));
		}

		//sso认证
		$ip = self::getonlineip();
		$param	= array(
			"user"	=>$username,
			"pwd"	=> strtoupper(md5($passwd)),
			"sub"	=> "1004211",
			"dyn"	=> $dyn,
			"ip"	=> $ip
		);

		$param_str	= "";
		$islogin = false;
		$isadmin = false;
		foreach ($param as $k=>$v) {
			$param_str	.= "$k=".urlencode($v)."&";
		}
		$url		= "http://61.172.241.94:8083/Tivoli/SsoCertify?";
		$url		.= $param_str;
		$text		= file_get_contents($url);
		$rows		= explode("|", $text);
		if ($rows[0]!="1")
		{
			$info	= iconv('GB18030','UTF-8',trim($rows[1]));//错误信息
			//查管理表
			$result		= $this->admin->getInfoByUsername($username);
			if (!$result)
			{
				die(json_encode(array("success" => true, "msg"=>$info)));
			}
			if ($result['password'] != md5($passwd))
			{
				die(json_encode(array("success"=>true,"msg"=>"密码错误")));
			}
			else
			{
				$islogin = true;
				$isadmin = true;
			}
		}
		else
		{
			$islogin = true;
		}

		if($islogin)
		{
			$username =  iconv('GB18030','UTF-8',$username);
			$userInfo = user_helper::getUserinfoByUserName($username);
			$username = $userInfo['realname'];
			$this->load->model('workers');
			$worker_info = $this->workers->getWorkerByName($username);
			if(!$worker_info)
			{
				$this->workers->addWorker($username);
				$group_id = 0;
			}
			else
			{
				$group_id = $worker_info['worker_groupid'];
			}
			//$userInfo = user_helper::getUserinfoByUserName($username);
			//$username = $userInfo['realname'];
			$_SESSION['username']	= $username;
			$_SESSION['worker_groupid'] = $group_id;
			$isadmin?$_SESSION['isadmin']=true:'';
			if(isset($RemberMe) && isset($RemberMePass)) { //判断是否要记录
				setcookie("remuser", $username, time()+86400*30); //设定用户名称的 Cookie 值
				setcookie("rempass", $passwd, time()+86400*30); //设定密码的 Cookie 值
			} else if(isset($RemberMe) && !isset($RemberMePass)) {
				setcookie("remuser", $username, time()+86400*30); //设定用户名称的 Cookie 值
				setcookie("rempass", '', time()); //去除密码的 Cookie 值
			} else if(isset($RemberMePass) && !isset($RemberMe)) {
				setcookie("rempass", $passwd, time()+86400*30); //设定密码的 Cookie 值
				setcookie("remuser", '', time()); //去除用户名称的 Cookie 值
			} else {
				setcookie("remuser", '', time()); //去除用户名称的 Cookie 值
				setcookie("rempass", '', time()); //去除密码的 Cookie 值
			}
			die(json_encode(array("success" => true, "msg"=>"ok")));
		}
	}

	public function admin()
	{
		$this->load->view('welcome_message');
	}
	static function getonlineip()
	{
		if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
			$onlineip = getenv('HTTP_CLIENT_IP');
		} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
			$onlineip = getenv('HTTP_X_FORWARDED_FOR');
		} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
			$onlineip = getenv('REMOTE_ADDR');
		} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
			$onlineip = $_SERVER['REMOTE_ADDR'];
		}
		preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
		return $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
	}

	public function menu()
	{
		$this->config->load("menu", true);
		$menu	= $this->config->item('menu');
		die(json_encode($menu['menu']));
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
